//
//  HMComposeView.swift
//  webo
//
//  Created by apple on 2016/11/19.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import pop
class HMComposeView: UIView {
// TODO: - 函数入口
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI(){
        let toolbar = UIToolbar()
        toolbar.frame = UIScreen.main.bounds
        self.addSubview(backview)
        self.addSubview(toolbar)
        self.addSubview(iv_ad)
        
        addIconButton()
        
        iv_ad.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(self).offset(100)
        }
    }
    // MARK: -将当前的视图添加到window上/根视图控制器的view上/传递控制器
    func show(fromvc: UIViewController){
        fromvc.view.addSubview(self)
        initialBtnAnimation()
    }
    
// MARK: - btn初始化动画
    func initialBtnAnimation(){
        for (i,btn) in composebtnArr.enumerated() {
            addComposeBtnAnimation(btn: btn, index: i, isEnd: false)
        }
    }
    // MARK: - 懒加载区域
  private  lazy var backview = UIImageView(image: UIImage.getCurrentImage())
  private  lazy var iv_ad = UIImageView(image: #imageLiteral(resourceName: "compose_slogan"))
  private  var composebtnArr:[HMComposeBtn] = [HMComposeBtn]()
    // MARK: - 添加8个按钮
   private func addIconButton(){
        let btnmargin = (ScreenWidth - composeIconbtnwidth * 3) / 4
    let imgs:[String] = ["tabbar_compose_idea","tabbar_compose_lbs","tabbar_compose_more","tabbar_compose_music","tabbar_compose_photo","tabbar_compose_review"];
    
    for i in 0..<6 {
            let col = i % 3
            let row = i / 3
            let X = CGFloat(col) * (composeIconbtnwidth + btnmargin) + btnmargin
            let Y = CGFloat(row) * (composeIconbtnwidth + btnmargin) + ScreenHeight
            let btn = HMComposeBtn()
            btn.setImage(UIImage.init(named: imgs[i]), for: .normal)
            btn.setTitle("good", for: .normal)
            btn .setTitleColor(#colorLiteral(red: 0.5725490451, green: 0, blue: 0.2313725501, alpha: 1), for: .normal)
            btn.tag = i
            btn.frame = CGRect(x:X, y: Y, width: composeIconbtnwidth, height: composeIconbtnheight)
            self.addSubview(btn)
            composebtnArr.append(btn)
            btn.addTarget(self, action: #selector(clickComposeBtn(clickbtn:)), for: .touchUpInside)
        }
        
    }
    // MARK: - 点击屏幕
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for (i, btn) in composebtnArr.reversed().enumerated(){
            addComposeBtnAnimation(btn: btn,index:i,isEnd: true)
        }
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {
            self.removeFromSuperview()
        }
    }
    // MARK: - 添加btn动画
    private func addComposeBtnAnimation(btn: HMComposeBtn,index: NSInteger,isEnd: Bool){
        let btnpopAnimation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
    btnpopAnimation?.springBounciness = 10.0
    btnpopAnimation?.springSpeed = 10
    btnpopAnimation?.beginTime = 0.025 * Double(index) + CACurrentMediaTime()
        let animationY = btn.center.y + (isEnd ? 350 : -350)
    btnpopAnimation?.toValue = CGPoint(x:btn.center.x, y: animationY)
    btn.pop_add(btnpopAnimation, forKey: nil)
    }
   
    // MARK: - 添加按钮点击事件
    @objc private func clickComposeBtn(clickbtn: HMComposeBtn){
        
        for btn in composebtnArr {
            
            if clickbtn.tag == btn.tag{
            UIView.animate(withDuration: 0.25, animations: {
                btn.transform = CGAffineTransform(scaleX: 2.0, y: 2.0)
            })
            }else{
            
            UIView.animate(withDuration: 0.25, animations: {
                btn.transform = CGAffineTransform(scaleX: 0.2 , y: 0.2 )
            btn.alpha = 0.1
            }, completion: { (_) in
                let bundleName = Bundle.main.infoDictionary!["CFBundleName"] as! String
                let className = bundleName + "." + "HMComposeViewController"
                guard let clsType = NSClassFromString(className) as? UIViewController.Type else{
                return
                }
                let vc = clsType.init()
                let navi = UINavigationController(rootViewController: vc)
                UIApplication.shared.keyWindow?.rootViewController?.present(navi, animated: true, completion: { 
                    self.removeFromSuperview()
                })
            })
            }
        }
    }
    
    
}



